package com.nanrui.mutualinductor.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.nanrui.mutualinductor.R;
import com.nanrui.mutualinductor.ui.adapter.RechargePhoneAdapter;
import com.ui.util.UIDisplayHelper;
import com.ui.util.UIResHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/5/23.
 */
public class CustomTitleDialog extends Dialog {


    public TextView subTvTitle;

    public Button buttonCancel;

    public GridView mGridView;

    private int mContentAreaMaxHeight = -1;

    private Context mContext;

    public CustomTitleDialog(Context context) {
        super(context, R.style.CustomDialog);
        init(context);
    }

    public CustomTitleDialog(Context context, int theme) {
        super(context, R.style.CustomDialog);
        init(context);
    }

    private List<String> mList;

    private RechargePhoneAdapter mAdapter;

    private void init(Context context) {

        mContext = context;
        setCancelable(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_custom_title, null);

        mGridView = view.findViewById(R.id.gv);
        mList = new ArrayList<>();
        mAdapter = new RechargePhoneAdapter(mContext, mList);
        mGridView.setAdapter(mAdapter);

        subTvTitle = (TextView) view.findViewById(R.id.tv_sub_tile);
        buttonCancel = (Button) view.findViewById(R.id.btn_cancel);
        setContentView(view);
    }

    public void setData(List<String> data) {
        mList.clear();
        mList.addAll(data);
        mAdapter.notifyDataSetChanged();
    }


    protected int getContentAreaMaxHeight() {
        if (mContentAreaMaxHeight == -1) {
            // 屏幕高度的0.85 - 预估的 title 和 action 高度
            return (int) (UIDisplayHelper.getScreenHeight(mContext) * 0.85) - UIDisplayHelper.dp2px(mContext, 100);
        }
        return mContentAreaMaxHeight;
    }

    public static void assignMessageTvWithAttr(TextView messageTv, boolean hasTitle, int defAttr) {
        UIResHelper.assignTextViewWithAttr(messageTv, defAttr);

        if (!hasTitle) {
            TypedArray a = messageTv.getContext().obtainStyledAttributes(null,
                    R.styleable.UIDialogMessageTvCustomDef, defAttr, 0);
            int count = a.getIndexCount();
            for (int i = 0; i < count; i++) {
                int attr = a.getIndex(i);
                if (attr == R.styleable.UIDialogMessageTvCustomDef_ui_paddingTopWhenNotTitle) {
                    messageTv.setPadding(
                            messageTv.getPaddingLeft(),
                            a.getDimensionPixelSize(attr, messageTv.getPaddingTop()),
                            messageTv.getPaddingRight(),
                            messageTv.getPaddingBottom()
                    );
                }
            }
            a.recycle();
        }
    }
}
